package com.appbee.lyrdb.client.ui;

import com.appbee.lyrdb.client.text.LyrStrings;
import com.appbee.lyrdb.shared.LyrSongProxy;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.CellTable.Resources;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.gwt.view.client.SingleSelectionModel;

public class LyrSongTable extends Composite
{

	private static LyrSongTableUiBinder uiBinder = GWT
			.create(LyrSongTableUiBinder.class);

	interface LyrSongTableUiBinder extends UiBinder<Widget, LyrSongTable>
	{
	}

	@UiField(provided=true)
	CellTable<LyrSongProxy> cellTable;
	
	
	@UiField(provided=true)
	SimplePager pager;

	private final SingleSelectionModel<LyrSongProxy> selectionModel = new SingleSelectionModel<LyrSongProxy>();

	private final LyrStrings lyrStrings;

	public LyrSongTable(int pageSize, Resources res, LyrStrings lyrStrings)
	{
		this.lyrStrings = lyrStrings;
		cellTable = new CellTable<LyrSongProxy>(pageSize,res);
		
		pager = new SimplePager();
		pager.setDisplay(cellTable);
		cellTable.setSelectionModel(selectionModel );
		addColumns();
		initWidget(uiBinder.createAndBindUi(this));
	}
	private void addColumns()
	{
		cellTable.addColumn(new TextCol()
		{
			
			@Override
			public String getValue(LyrSongProxy object)
			{
				return object.getName();
			}
		}, lyrStrings.name());
		cellTable.addColumn(new TextCol()
		{
			
			@Override
			public String getValue(LyrSongProxy object)
			{
				return object.getArtist();
			}
		}, lyrStrings.artist());
	}
	
	public HasData<LyrSongProxy> getHasData()
	{
		return cellTable;
	}
	
	public void addSelectionChangeHandler(Handler handler)
	{
		selectionModel.addSelectionChangeHandler(handler);
	}

	public LyrSongProxy getSelectedSong()
	{
		return selectionModel.getSelectedObject();
	}
	
	private abstract class TextCol extends Column<LyrSongProxy, String>
	{
		public TextCol()
		{
			super(new TextCell());
		}
		
	}
	
}
